package com.example.module_service.viewmodel

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.example.lib_common.view.entity.AddPointEntity
import com.example.lib_common.view.entity.CarAddEntity
import com.example.lib_common.view.entity.CarAddParam
import com.example.lib_common.view.entity.CarPayEntity
import com.example.lib_common.view.entity.CarPayParam
import com.example.lib_common.view.entity.CarStopEntityItem
import com.example.lib_common.view.entity.CarplateEntity
import com.example.lib_common.view.entity.DeviceDetailEntity
import com.example.lib_common.view.entity.DeviceEntity
import com.example.lib_common.view.entity.PatrolEntity
import com.example.lib_common.view.entity.PatrolTaskBeanItem
import com.example.lib_common.view.entity.PatrolsBeanItem
import com.example.lib_common.view.vm.BaseViewModel
import com.example.module_service.park.ParkBeanItem
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class ServerViewModel @Inject constructor(private val repo:ServerRepository):BaseViewModel(){
    val mSuccess= MutableLiveData<List<ParkBeanItem>>()
    val DeviceSuccess= MutableLiveData<List<DeviceEntity>>()
    val DeviceDetailSuccess= MutableLiveData<List<DeviceDetailEntity>>()
    val CarSUccess= MutableLiveData<List<CarplateEntity>>()
    val addCar = MutableLiveData<CarAddEntity>()

    val getCarPay = MutableLiveData<CarPayEntity>()
    val getcarStop= MutableLiveData<List<CarStopEntityItem>>()

    val getpatrolSuccess= MutableLiveData<List<PatrolEntity>>()

    val addPointSuccess= MutableLiveData<AddPointEntity>()
    var getPointById = MutableLiveData<PatrolsBeanItem>()
    var getTasksBId = MutableLiveData<PatrolTaskBeanItem>()
    //getPatrolTaskByStatus
    val getPatrolTaskBySuccess= MutableLiveData<List<PatrolTaskBeanItem>>()

    //  详情
    val getTaskDetailSuccess= MutableLiveData<PatrolTaskBeanItem>()
    fun getPark(areaId:Int){
        //使用线程池
        mViewModelScope.launch(mDispatcher){
            val (code,data ,message )=repo.getPark(areaId)
            if(code==200){
                mSuccess.postValue(data)
            }else{
                Log.e("ServerViewModel", "Error code: $code, message: $message")
                mError.postValue(message)
            }
        }
    }

    fun getDevice(){
        mViewModelScope.launch(mDispatcher) {
            val (code,data,message) =repo.getDevice()
            if(code==200){
                DeviceSuccess.postValue(data)
            }else{
                Log.e("ServerViewModel", "Error code: $code, message: $message")
                mError.postValue(message)
            }
        }
    }

    fun getDeviceDetail(id:Int){
        mViewModelScope.launch(mDispatcher) {
            val (code,data,message) =repo.getDeviceDetail(id)
            if(code==200){
                DeviceDetailSuccess.postValue(listOf(data))
            }else{
                Log.e("ServerViewModel", "Error code: $code, message: $message")
                mError.postValue(message)
            }
        }
    }
    fun getProvince(){
        mViewModelScope.launch(mDispatcher) {
            val (code,data,message) =repo.getProvince()
            if(code==200){
                CarSUccess.postValue(data)
            }else{
                Log.e("ServerViewModel", "Error code: $code, message: $message")
                mError.postValue(message)
            }
        }
    }
    fun getAddCar(carAddParam: CarAddParam){
        mViewModelScope.launch(mDispatcher) {
            val (code,data,message) =repo.getAddCar(carAddParam)
            if(code==200){
                addCar.postValue(data)
            }else{
                Log.e("ServerViewModel", "Error code: $code, message: $message")
                mError.postValue(message)
            }
        }
    }

    fun getAddCarPay(carPayParam: CarPayParam){
        mViewModelScope.launch {
            val (code,data,message) =repo.getAddCarPay(carPayParam)
            if(code==200){
                getCarPay.postValue(data)
            }else{
                Log.e("ServerViewModel", "Error code: $code, message: $message")
                mError.postValue(message)
            }
        }
    }
    fun GetCarStop(){
        mViewModelScope.launch {
            val (code,data,message) =repo.getCarStop()
            if(code==200){
                getcarStop.postValue(data)
            }else{
                Log.e("ServerViewModel", "Error code: $code, message: $message")
                mError.postValue(message)
            }
        }
    }
    fun getPatrol(){
        mViewModelScope.launch {
            val (code,data,message) =repo.getPatrol()
            if(code==200){
                getpatrolSuccess.postValue(data)
            }else{
                Log.e("ServerViewModel", "Error code: $code, message: $message")
                mError.postValue(message)
            }
        }
    }
    fun addPoint(addPointEntity: AddPointEntity){
        mViewModelScope.launch {
            val (code,data,message) =repo.addPoint(addPointEntity)
            if(code==200){
                addPointSuccess.postValue(data)
            }else{
                Log.e("ServerViewModel", "Error code: $code, message: $message")
                mError.postValue(message)
            }
        }
    }


    //getPatrolTaskByStatus
    fun getPatrolTaskByStatus(status:String){
        mViewModelScope.launch {
            val (code,data,message) =repo.getPatrolTaskByStatus(status)
            if(code==200){
                getPatrolTaskBySuccess.postValue(data)
            }else{
                Log.e("ServerViewModel", "Error code: $code, message: $message")
                mError.postValue(message)
            }
        }
    }

    //getTaskById
    fun getTaskById(id:Int){
        mViewModelScope.launch {
            val (code,data,message) =repo.getTaskById(id)
            if(code==200){
                getTaskDetailSuccess.postValue(data)
            }else{
                Log.e("ServerViewModel", "Error code: $code, message: $message")
                mError.postValue(message)
            }
        }
    }

}